#!/bin/bash

echo "Searching..."
find ../ | grep -v '.svn' | grep -v 'Imaging' | grep -v 'Soar' | grep -v "`basename $0`" | grep -v '/obj/' > conventions.files
echo "  `cat conventions.files | wc -l` files found"

function Check() {
  E=`echo $@ | sed 's/ /\n/g' | wc -l`
  if [ "$1" != "" ]; then
    echo "$E ERRORS !"
    echo "$E ERRORS !" >> conventions.log
    echo "$@" | sed 's/ /\n/g' | sed 's/^\.\./  \.\./'
    echo "$@" | sed 's/ /\n/g' | sed 's/^\.\./  \.\./' >> conventions.log
  fi
  echo
  echo >> conventions.log
}

> conventions.log

echo "---------------------------------- todo ---------------------------------------"

for i in `cat conventions.files | grep -e '.pas' -e '.dpr'`; do
  S=`cat $i | grep -e 'FIXME' -e 'TODO' -e 'WARNING' | sed 's/  //g;s/\t//g;s/\/\//\n  /g'`
  if [ "$S" != "" ]; then
    echo
    echo >> conventions.log
    echo "$i:"
    echo "$i:" >> conventions.log
    echo "$S" | sed 's/FIXME/\n\nFIXME/g;s/TODO/\n\nTODO/g;s/WARNING/\n\nWARNING/g;s/^ //g' | grep -v '^$' | grep -v '^ $'
    echo "$S" | sed 's/FIXME/\n\nFIXME/g;s/TODO/\n\nTODO/g;s/WARNING/\n\nWARNING/g;s/^ //g' | grep -v '^$' | grep -v '^ $' >> conventions.log
  fi
done
echo

echo "------------------------------- conventions -----------------------------------"

echo -n "Checking spaces in filenames: "
Check `cat conventions.files | grep ' '`

echo -n "Checking minus in filenames: "
Check `cat conventions.files | grep '-'`

echo -n "Backup files: "
Check `cat conventions.files | grep '~'`

echo -n "Capital letters in filenames: "
Check `cat conventions.files | grep -e 'A' -e 'B' -e 'C' -e 'D' -e 'E' -e 'F' -e 'G' -e 'H' -e 'I' -e 'J' -e 'K' -e 'L' -e 'M' -e 'N' -e 'O' -e 'P' -e 'Q' -e 'R' -e 'S' -e 'T' -e 'U' -e 'V' -e 'W' -e 'X' -e 'Y' -e 'Z'`

echo "Capitalized generics: "
for i in `cat conventions.files | grep -v 'Imaging' | grep -v 'Soar' | grep -v 'delphi' | grep -e '\.pas' -e '\.dpr'`; do
  V=`cat $i | grep -e 'String' -e 'Integer' -e 'Word' -e 'Pointer' -e 'For ' -e 'Boolean' -e 'Real' -e ' Array' -e ':Array' -e 'Char' | grep -v 'glGetIntegerv' | grep -v 'CharArr' | grep -v 'TString' | grep -v 'xPointer' | grep -v PChar | grep -v ImageDataPointer`
  E=`echo "$V" | wc -l`
  if [ "$E" != "0" ] && [ "$V" != "" ]; then    
    echo "  $i: $V"
    echo "  $i: $V" >> conventions.log
 fi  
done

echo "TAB indentation: "
for i in `cat conventions.files | grep -v 'Imaging' | grep -v 'Soar' | grep -v 'delphi' | grep -e '\.pas' -e '\.dpr'`; do
  V=`cat $i | sed 's/\t/__TAB__/g' | grep '__TAB__'`
  E=`echo "$V" | wc -l`
  if [ "$E" != "0" ] && [ "$V" != "" ]; then    
    echo "  $i: $V"
    echo "  $i: $V" >> conventions.log
  fi  
done

echo
echo "DONE, PRESS ENTER"
read